<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色信息">
          <el-button type="primary" @click="addClick">+新增角色</el-button>
          <!-- 表格 -->
          <el-table border :data="list">
            <el-table-column label="序号" type="index">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="scope">
                <div>
                  <el-button
                    type="text"
                    @click="powerClick(scope.row)"
                  >分配权限</el-button>
                  <el-button type="text" @click="edit(scope)">修改</el-button>
                  <el-button
                    type="text"
                    @click="delClick(scope)"
                  >删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="currentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <component :is="'Info'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- v-model数据双向绑定 -->
    <!-- <DialogItem :is-show="show" @input="show = $event" /> -->
    <!-- <DialogItem v-model="show" /> -->
    <!-- .sync数据双向绑定 -->
    <!-- <DialogItem :is-show="show" @update:isShow="show = $event" /> -->
    <DialogItem
      ref="dialogItem"
      :is-show.sync="show"
      :list="list"
      @currentChange="currentChange"
      @getData="getData"
    />
    <!-- 权限分配 -->
    <DialogPower ref="dialogPower" v-model="powerShow" />
  </div>
</template>

<script>
import DialogItem from './components/dialogItem.vue'
import { sysRole, sysRoleDel } from '@/api/setting.js'
import DialogPower from './components/dialogPower.vue'
export default {
  components: {
    DialogItem,
    DialogPower,
    Info: () => import('./components/info.vue')
  },
  data() {
    return {
      powerShow: false,
      show: false,
      list: [],
      page: {
        // 当前页数，支持 .sync 修饰符
        page: 1,
        // 每页显示条目个数，支持 .sync 修饰符
        pagesize: 1,
        // 总条目数
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取数据
    async getData() {
      const res = await sysRole(this.page)
      // 存储数据
      this.list = res.data.rows
      this.page.total = res.data.total
      // console.log(res)
    },
    // 修改每页容量个数
    sizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    // 修改页
    currentChange(page) {
      // console.log(page)
      this.page.page = page
      // 页数修改后重新调用接口
      this.getData()
    },
    addClick() {
      this.show = true
    },
    // 删除按钮
    delClick(scope) {
      this.$confirm('确认要删除吗?', '提示').then(async() => {
        await sysRoleDel(scope.row.id)
        this.$message.success('删除成功')
        // 删除成功后重新调用接口
        this.getData()
      })
      // console.log('scope', scope)
    },
    // 修改按钮
    edit(scope) {
      // 打开弹窗
      this.show = true
      // 把当前项的数据深拷贝给子页面的form
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(scope.row))
      // 给dialogItem 的mode改为edit 表示打开的是编辑页面
      this.$refs.dialogItem.mode = 'edit'
    },
    powerClick(row) {
      // console.log(row)
      this.$refs.dialogPower.getRoleInfo(row.id)
      this.powerShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 20px;
}
</style>
